package com.jbm.jbmsupplier.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import org.xmlpull.v1.XmlSerializer;

import android.os.Environment;
import android.util.Log;
import android.util.Xml;

import com.jbm.jbmsupplier.model.*;

public class XmlWrite {
	public static String userFile = Environment.getExternalStorageDirectory()+File.separator+"mediafileInfo"+File.separator+"jbm_user.xml";
	StringBuilder sb = new StringBuilder();
	public XmlWrite() {
		// TODO Auto-generated constructor stub
	}

	public boolean writeXml(JobInfo job) {
		String xmlFileName = job.getXmlFileName();
		StringBuilder tempSb = new StringBuilder();
		File xmlFile = new File(xmlFileName);
		try {
			xmlFile.createNewFile();
		} catch (IOException e) {
			Log.e("IOException", "exception in createNewFile() method");
			return false;
		}

		FileOutputStream fileos = null;
		try {
			fileos = new FileOutputStream(xmlFile);
		} catch (FileNotFoundException e) {
			Log.e("FileNotFoundException", "can't create FileOutputStream");
			return false;
		}	

		XmlSerializer serializer = Xml.newSerializer();
		try {
			serializer.setOutput(fileos, "UTF-8");
			serializer.startDocument(null, Boolean.valueOf(true));
			serializer.setFeature(
				"http://xmlpull.org/v1/doc/features.html#indent-output",
				true);
			serializer.startTag(null, "files");
			for(int fileIndex = 0 ;fileIndex<job.getFileCount(); fileIndex++)
			{
				fileInfo fInfo = job.getFileInfo(fileIndex);
				serializer.startTag(null, "fileInfo");
				serializer.attribute(null, "file_name", fInfo.getFileName());
				serializer.attribute(null, "path_name", fInfo.getPathName());
				serializer.attribute(null, "file_type", Integer.toString(fInfo.getType()));
				serializer.attribute(null, "file_dec", fInfo.getDec());
				serializer.attribute(null, "create_time", fInfo.getDate());
				if(fInfo.getText()== null)
				{
					serializer.attribute(null, "text", "");
				}
				else
				{
					serializer.attribute(null, "text", fInfo.getText());
				}
				serializer.endTag(null, "fileInfo");
			}
			serializer.endTag(null, "files");
			serializer.endDocument();
			serializer.flush();
			fileos.close();
			tempSb.append("file has been created");
		} catch (Exception e) {
			Log.e("Exception", "error occurred while creating xml file");
			tempSb.append("Create file error");
			if(fileos != null)
				try
				{
					fileos.close();
				}
				catch(Exception ex){return false;}
			return false;
		}
		return true;		
	}
	
	public boolean WriteUserXML()
	{
		String xmlFileName = XmlWrite.userFile;
		StringBuilder tempSb = new StringBuilder();
		File xmlFile = new File(xmlFileName);
		try {
			xmlFile.createNewFile();
		} catch (IOException e) {
			Log.e("IOException", "exception in createNewFile() method");
			return false;
		}

		FileOutputStream fileos = null;
		try {
			fileos = new FileOutputStream(xmlFile);
		} catch (FileNotFoundException e) {
			Log.e("FileNotFoundException", "can't create FileOutputStream");
			return false;
		}	

		XmlSerializer serializer = Xml.newSerializer();
		try {
			serializer.setOutput(fileos, "UTF-8");
			serializer.startDocument(null, Boolean.valueOf(true));
			serializer.setFeature(
				"http://xmlpull.org/v1/doc/features.html#indent-output",
				true);
			serializer.startTag(null, "UserInfo");
			serializer.attribute(null, "name", XmlRead.getUserName());
			serializer.attribute(null, "password", XmlRead.getUserPwd());
			serializer.endTag(null, "UserInfo");
			serializer.endDocument();
			serializer.flush();
			fileos.close();
			tempSb.append("file has been created");
		} catch (Exception e) {
			Log.e("Exception", "error occurred while creating xml file");
			tempSb.append("Create file error");
			if(fileos != null)
				try
				{
					fileos.close();
				}
				catch(Exception ex){return false;}
			return false;
		}
		return true;		
	}
}
